/**
 * Created by liubin on 2017/2/28.
 */



var WhmjReplayLayer = GameCCSBase.extend({
    mSpeed: null,
    mSpeedType: null,
    mSpeedTemp: null,
    mIsPlay: null,
    mSpeedTypes: null,

    Text_replaySpeed: null,
    ctor: function () {
        this._super();
        this.loadCocostudio("res/ccs/WhmjReplayLayer.json");
        this.mSpeed = 3.0;
        this.mSpeedTypeIndex = 1;
        this.mSpeedTypes = [1, 2, 4, 6, 8];
        this.mIsPlay = false;
        this.Text_replaySpeed = mo.seekWidgetByName(this.ccsNode, "Text_replaySpeed");

        mo.seekWidgetByName(this.ccsNode, "Button_replayExit").addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEnd(type)) {
                this.onPlayEnd();
            }
        }, this);

        mo.seekWidgetByName(this.ccsNode, "Button_replayLast").addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEnd(type)) {
                this.onPlayLast();
            }
        }, this);

        mo.seekWidgetByName(this.ccsNode, "Button_replayPlay").addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEnd(type)) {
                if (this.mIsPlay) {
                    this.onStop();
                } else {
                    this.onStart();
                }
            }
        }, this);

        mo.seekWidgetByName(this.ccsNode, "Button_replayNext").addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEnd(type)) {
                this.onPlayNext();
            }
        }, this);

        //分享录像
        mo.seekWidgetByName(this.ccsNode, "Button_replayIdShare").addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEnd(type)) {
                mo.whmjTabel.onShareReplay();
            }
        }, this);

        this.updateSpeedText();
    },

    setReplayId: function (replayId) {
        mo.gameHelper.setTextString(mo.seekWidgetByName(this.ccsNode, "Text_replayId"), "分享录像(" + replayId.substr(7) + ")");
    },

    updateReplay: function (dt) {
        this.mSpeedTemp += dt;
        if (this.mSpeedTemp > this.getSpeed()) {
            //播放
            this.mSpeedTemp = 0;
            var isPlay = mo.whmjTableStatus.playReplay();
            if (isPlay) {
                //播放了一次
            } else {
                //播放结束
                this.onStop();
            }
        }
    },

    onStart: function () {
        this.mIsPlay = true;
        this.mSpeedTemp = 0;

        this.unschedule(this.updateReplay);
        this.schedule(this.updateReplay);

        this.updateSpeedText();
    },

    onStop: function () {
        this.mIsPlay = false;
        this.unschedule(this.updateReplay);

        this.updateSpeedText();
    },

    onPlayEnd: function () {
        this.onStop();
        mo.sceneHelper.intoHallLayer();
    },

    updateSpeedText: function () {
        var speed = this.mSpeedTypes[this.mSpeedTypeIndex];
        mo.gameHelper.setTextString(this.Text_replaySpeed, speed + "X");

        if (this.mIsPlay) {
            mo.seekWidgetByName(this.ccsNode, "Button_replayPlay").loadTextures("res/ccs/whmj/table/replay/pause_searchpai.png", "res/ccs/whmj/table/replay/pause_searchpai.png");
        } else {
            mo.seekWidgetByName(this.ccsNode, "Button_replayPlay").loadTextures("res/ccs/whmj/table/replay/play_searchpai.png", "res/ccs/whmj/table/replay/play_searchpai.png");
        }

    },

    onPlayNext: function () {
        var index = this.mSpeedTypeIndex + 1;
        if (index < this.mSpeedTypes.length) {
            this.mSpeedTypeIndex = index;
        } else {

        }

        this.updateSpeedText();
    },

    onPlayLast: function () {
        var index = this.mSpeedTypeIndex - 1;
        if (index >= 0) {
            this.mSpeedTypeIndex = index;
        } else {

        }

        this.updateSpeedText();
    },

    getSpeed: function () {
        return this.mSpeed / this.mSpeedTypes[this.mSpeedTypeIndex];
    }


});
